<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>父组件访问子组件数据</title>
</head>
<body>
<div id="app">
    <cpn></cpn>
    <cpn1 ref="cpn1"></cpn1>
    <cpn2 ref="cpn2"></cpn2>
    <button v-on:click="cpn1expose">cpn1 暴漏内容</button>
    <button v-on:click="cpn2expose">cpn2 暴漏内容</button>
</div>

<template id="cpn">
    <div>子组件内容</div>
</template>

<script type="module">
    import {createApp, defineComponent, ref} from "../../../js/vue.esm-browser.js";

    let component = defineComponent(
        {
            template: "#cpn",
        }
    );

    let component1 = defineComponent(
        {
            template: "#cpn",
            setup(props, context) {
                // console.log(props);
                // console.log(context);

                let name = ref("我是来自 component1 中的 name")

                function showMessage() {
                    console.log("我是来自 component1 中的 showMessage");
                }

                // 在子组件中定义暴漏内容
                context.expose(
                    {
                        name, showMessage,
                    }
                )

                return {};
            }
        }
    );

    let component2 = defineComponent(
        {
            template: "#cpn",
            // 将 第二个参数 context 解构，获得需要处理的 expose
            setup(props, {expose}) {
                // console.log(props);
                // console.log(context);

                let name = ref("我是来自 component2 中的 name")

                function showMessage() {
                    console.log("我是来自 component2 中的 showMessage");
                }

                // 在子组件中定义暴漏内容
                expose(
                    {
                        name, showMessage,
                    }
                )

                return {};
            }
        }
    );


    let app = createApp(
        {
            setup() {
                let cpn1 = ref(null)

                function cpn1expose() {
                    console.log(cpn1);
                    console.log(cpn1.value);
                    console.log(cpn1.value.name);
                    cpn1.value.showMessage();
                }

                let cpn2 = ref(null)

                function cpn2expose() {
                    console.log(cpn2);
                    console.log(cpn2.value);
                    console.log(cpn2.value.name);
                    cpn2.value.showMessage();
                }

                return {
                    cpn1, cpn1expose, cpn2, cpn2expose,
                }
            }
        }
    );

    app.component("cpn", component)
    app.component("cpn1", component1)
    app.component("cpn2", component2)


    app.mount("#app")
</script>
</body>
</html>